/*
 * Copyright (c) 2009, artur
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the author nor the names of any co-contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/*
 * sys/alphabet/kmm.cpp
 *
 *  Created on: Dec 24, 2009
 *      Author: artur
 */

#include <kiddie/kiddie.h>
#include <sys/types.h>
#include <sys/mem.h>
#include <sys/errno.h>
#include <platform.h>

#include <Console.h>
#include <hw/Platform.h>

static struct m_block used_kmem_head;
static struct m_block free_kmem_head;

extern kernel::Console cout;
extern kernel::Platform hwplatform;

void *
operator new[](size_t size)
{
//  cout << "[Global] void *operator new[](size_t size)\n";
  return operator new(size);
}

/**
 * Allocates 'size' bytes in 'memtype' memory space.
 * @param size
 * @param memtype
 * @return pointer to new allocated memory block or NULL
 */
void *
operator new(size_t size, int memtype)
{

}

void *
operator new(size_t size, bool clean)
{
  void *r = operator new(size);
  if (clean && r)
    {
      util::memset(r, 0, size);
    }
  return r;
}

/**
 * TODO Take attention to 'flags' parameter
 */
void *
operator new(unsigned int size)
{
  void *res = ::hwplatform.vmFactory.alloc(size, 0, 0, 0);
//  cout << kernel::Console::HEX;
//  cout << "[Global] void *operator new[](size_t size) = " << (int) res << "\n";
  return res;
}

/**
 * TODO Implement global 'delete' operator.
 */
void
operator delete(void *addr)
{
  //	::cout << "[Global] void *operator delete(void *)";
}

/**
 * TODO Implement global 'delete[]' operator.
 */
void
operator delete[](void *addr)
{
  //	::cout << "[Global] void *operator delete(void *)";
}

void *
util::memset(void *s, char c, size_t n)
{
  size_t k;
  for (k = 0; k < n; k++)
    {
      ((char *) s)[k] = c;
    }
  return s;
}

void *
util::memsetw(void *s, const unsigned short w, size_t n)
{
  size_t k;
  for (k = 0; k < n; k++)
    {
      ((unsigned short *) s)[k] = w;
    }
  return s;
}

void *
util::memcpy(void *dest, const unsigned char *src, size_t n)
{
  size_t k;
  for (k = 0; k < n; k++)
    {
      ((char *) dest)[k] = ((char *) src)[k];
    }
  return dest;
}

/**
 * TODO Create special class for util functions 
 */
int
util::strncmp(const char *s, const char *t, size_t n)
{
  int res = 1;

  if (!n)
    return 0;

  for (; n-- && *s != 0 && *t != 0; s++, t++)
    {
      res = *s - *t;
      if (res)
        break;
    }

  return res;
}

/**
 * TODO Create special class for util functions 
 */
int
util::strcmp(const char *s, const char *t)
{
  for (; *s == *t; s++, t++)
    {
      if (*s == '\0')
        return 0;
    }
  return *s - *t;
}
